#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "gm_wifi.h"
#include "Rk_wifi.h"

int is_paring_success_flag() {
   if (access("/data/wifi_conncect_success", F_OK) != -1) { 
       printf("is_paring_success_flag exists.\n");
       return 0;
   } else {
       printf("is_paring_success_flag not exist.\n");
       return -1;
   }
}

void get_save_ssid_pwd(char *ssid, char *pwd) {
    char ssid_pwd[128] = {0};
    FILE *fp;
    fp = fopen("/data/wifi_conncect_success", "r+");
    fread(ssid_pwd, sizeof(ssid_pwd), 1, fp);
    fclose(fp);
    
    printf("wqy ssid_pwd = %s\n", ssid_pwd);
    char division[2] = "|";
    char *token;
    token = strtok(ssid_pwd, division);
    printf("ssid = %s\n", token);
    strcpy(ssid, token);

    token = strtok(NULL, division);
    printf("pwd = %s\n", token);
    strcpy(pwd, token);
}


void set_save_ssid_pwd(char *ssid, char *pwd) {
    char save_ssid_pwd_cmd[128] = {0};
    sprintf(save_ssid_pwd_cmd, "%s|%s", ssid, pwd);

    FILE *fp;
    fp = fopen("/data/wifi_conncect_success", "w+");
    fwrite(save_ssid_pwd_cmd, strlen(save_ssid_pwd_cmd) + 1, 1, fp);
    fclose(fp);
}

/*********wifi*********************/
static RK_WIFI_RUNNING_State_e g_wifi_state = RK_WIFI_State_OFF;
static int rk_wifi_state_callback(RK_WIFI_RUNNING_State_e state, RK_WIFI_INFO_Connection_s *info)
{
	printf("%s state: %d\n", __func__, state);
	//printf_wifi_link_info(info);

	g_wifi_state = state;
	if (state == RK_WIFI_State_CONNECTED) {
		printf("RK_WIFI_State_CONNECTED\n");
	} else if (state == RK_WIFI_State_CONNECTFAILED) {
		printf("RK_WIFI_State_CONNECTFAILED\n");
	} else if (state == RK_WIFI_State_CONNECTFAILED_WRONG_KEY) {
		printf("RK_WIFI_State_CONNECTFAILED_WRONG_KEY\n");
	} else if (state == RK_WIFI_State_OPEN) {
		printf("RK_WIFI_State_OPEN\n");
	} else if (state == RK_WIFI_State_OFF) {
		printf("RK_WIFI_State_OFF\n");
	} else if (state == RK_WIFI_State_DISCONNECTED) {
		printf("RK_WIFI_State_DISCONNECTED\n");
	} else if (state == RK_WIFI_State_SCAN_RESULTS) {
		char *scan_r;
		printf("RK_WIFI_State_SCAN_RESULTS\n");
		scan_r = RK_wifi_scan_r();
		free(scan_r);
	} else if (state == RK_WIFI_State_LP_EVENT_TYPE) {
		if (info->lp_event_type == RK_WIFI_LP_BAT_LEVEL) {
			printf("RK_WIFI_MCU_BAT_LEVEL: %d\n", info->lp_data[0]);
		} else if (info->lp_event_type == RK_WIFI_LP_PIR_DETECT) {
			printf("RK_WIFI_MCU_PIR_DETECT: %d\n", info->lp_data[0]);
		} else if (info->lp_event_type == RK_WIFI_LP_LONG_PRESS) {
			printf("RK_WIFI_MCU_LONG_PRESS_KEY\n");
		} else if (info->lp_event_type == RK_WIFI_LP_SHORT_PRESS) {
			printf("RK_WIFI_MCU_SHORT_PRESS_KEY\n");
		} else if (info->lp_event_type == RK_WIFI_LP_KEEPALIVE_OK) {
			printf("RK_WIFI_MCU_KEEPALIVE_OK\n");
		} else if (info->lp_event_type == RK_WIFI_LP_KEEPALIVE_FAILED) {
			printf("RK_WIFI_MCU_KEEPALIVE_FAILED\n");
		} else if (info->lp_event_type == RK_WIFI_LP_WAKEUP_REASON) {
			printf("RK_WIFI_LP_WAKEUP_REASON: %d\n", info->lp_data[0]);
		}
	}

	return 0;
}

int dhcp_get_ip() {
	printf("dhcp_get_ip\n");

	int timeout = 10; //1000 * 10ms == 10s
	char find_udhcpd_cmd[64] = "";
	while (timeout--) {
		usleep(1000 * 1000);

		//udhcpc -i wlan0
		char dhcp_cmd[64] = "udhcpc -i wlan0";
		int ret = system_cmd(dhcp_cmd);
		if (ret == 0) {
			printf("udhcp success\n");
			usleep(10 * 1000); //delay 10ms
			return 0;
		} else {
			printf("udhcp failed");
		}
	}
}

void start_wifi() {
	printf("start_wifi\n");
	RK_wifi_register_callback(rk_wifi_state_callback);
	if (!RK_wifi_enable(1))
	printf("RK_wifi_enable 1 fail!\n");

	int timeout = 200; //200 * 10ms == 2s
	while ((timeout--) && (g_wifi_state == RK_WIFI_State_OFF)) {
		usleep(10 * 1000);
		printf("%s: TURNING ON ...\n", __func__);
	}

	if (g_wifi_state == RK_WIFI_State_OFF) {
		printf("wifi is still off");
	}
}

int connect_wifi(char *ssid, char *pwd) {
	printf("connect_wifi, ssid = %s, pwd = %s\n", ssid, pwd);
	if (!RK_wifi_connect(ssid, pwd)) {
		printf("rk_wifi_connect fail!\n");
		return -1;
	}

	int timeout = 800; //500 * 10ms == 5s
	while (timeout--) {
		usleep(10 * 1000);
		if (g_wifi_state == RK_WIFI_State_DHCP_OK) {
			printf("rk_wifi_connect success\n");
			return 0;
		} else if (g_wifi_state == RK_WIFI_State_CONNECTFAILED_WRONG_KEY) {
			printf("rk_wifi_connect failed, wrong key\n");
			return -2;
		} else if (g_wifi_state == RK_WIFI_State_CONNECTFAILED) {
			printf("rk_wifi_connect failed\n");
			return -1;
		}
	}

	printf("rk_wifi_connect timeout 5s\n");
	return -1;
}